package org.sbbs.app.demo.webapp.action;

import javax.validation.Valid;

import org.sbbs.app.demo.model.DemoEntity;
import org.sbbs.app.demo.service.DemoEntityManager;
import org.sbbs.base.webapp.action.BaseMaintainAction;

public class DemoDwzMaintainAction
    extends BaseMaintainAction<DemoEntity, Long> {

    /**
     *
     */
    private static final long serialVersionUID = 2332788763668956953L;

    public String add() {
        try {
            this.model = new DemoEntity();
            this.setEditTypeAdd();
        }
        catch ( Exception e ) {
            this.setMessage( getText( "error.common", new String[] { e.getMessage() } ) );
            this.setStatusCode( AJAX_STATUS_ERROR );

        }
        return SUCCESS;
    }

    public String edit() {
        try {
            this.model = this.demoEntityManager.find( this.getId() );
            this.setEditTypeEdit();
        }
        catch ( Exception e ) {
            this.setMessage( getText( "error.common", new String[] { e.getMessage() } ) );
            this.setStatusCode( AJAX_STATUS_ERROR );
        }
        return SUCCESS;
    }

    public String save() {
        try {
            this.demoEntityManager.save( this.getModel() );
            this.setMessage( getText( ( isNew() ) ? "demoEntity.added" : "demoEntity.updated", "no msg key found,save successed." ) );
            this.setStatusCode( AJAX_STATUS_SUCCESS );
            this.setCallbackType( CALLBACKTYPE_CLOSECURRENT );
        }
        catch ( Exception e ) {
            this.setMessage( getText( "error.saved", new String[] { e.getMessage() } ) );
            this.setStatusCode( AJAX_STATUS_ERROR );
        }
        return SUCCESS;
    }

    public String delete() {
        try {
            String[] sIds = this.getIds().split( "," );

            Long[] lIds = new Long[sIds.length];
            for ( int i = 0; i < sIds.length; i++ ) {
                lIds[i] = Long.parseLong( sIds[i] );
            }

            this.demoEntityManager.removeByIds( lIds );

            this.setMessage( getText( "demoEntity.deleted", "no msg key found,delete successed." ) );
            this.setStatusCode( AJAX_STATUS_SUCCESS );
        }
        catch ( Exception e ) {
            this.setMessage( getText( "error.deleted", new String[] { e.getMessage() } ) );
            this.setStatusCode( AJAX_STATUS_ERROR );
        }

        return SUCCESS;
    }

    public String exportExcel() {
        return SUCCESS;
    }

    private DemoEntityManager demoEntityManager;

    @Valid
    private DemoEntity model;

    public DemoEntityManager getDemoEntityManager() {
        return demoEntityManager;
    }

    public void setDemoEntityManager( DemoEntityManager demoEntityManager ) {
        this.demoEntityManager = demoEntityManager;
    }

    public DemoEntity getModel() {
        return model;
    }

    public void setModel( DemoEntity model ) {
        this.model = model;
    }

}
